package com.company.project.util;

import com.alibaba.fastjson.annotation.JSONField;

import java.util.ArrayList;
import java.util.List;

public class PageUtil<T> {

    @JSONField(serialize=false)
    private int start; // 启始条数

    private int totalPage; // 总页数

    @JSONField(serialize=false)
    private Integer cpage = 1; // 当前页

    @JSONField(serialize=false)
    private long totalRow; // 总信息数

    @JSONField(serialize=false)
    private int pageSize = 5; // 分页单位


    private List<T> lists = new ArrayList<>(); //数据

    public int getStart() {
        return start;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public int getCpage() {
        return cpage;
    }

    public long getTotalRow() {
        return totalRow;
    }

    public int getPageSize() {
        return pageSize;
    }

    public List<T> getLists() { return lists; }

    public void setStart(int start) {
        this.start = start;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public void setCpage(Integer cpage) {
        if (cpage ==null || cpage<=0){
            this.cpage = 1;
            return;
        }
        this.cpage = cpage;
    }

    public void setTotalRow(long totalRow) {
        this.totalRow = totalRow;
        this.totalPage = (int) (totalRow / pageSize);
        if (totalRow % pageSize > 0) {
            this.totalPage = this.totalPage + 1;
        }

        if (this.cpage > this.totalPage) {
            this.cpage = this.totalPage;
        }
        if (totalPage == 0) {
            this.cpage = 1;
        }
        this.start = (this.cpage - 1) * pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public void setLists(List<T> lists) { this.lists = lists; }


}
